package com.music.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.music.dao.SongListMapper;
import com.music.entity.SongList;
import com.music.entity.SongListType;
import com.music.service.SongListService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
@Slf4j
public class SongListServiceImpl implements SongListService {
    Logger logger= LoggerFactory.getLogger(SongListServiceImpl.class);

    @Autowired
    public SongListMapper songListMapper;

    @Override
    public PageInfo<SongList> getSongListDetailByPage(SongList songList, int pageNum, int pageSize,String rank) {
        if(songList != null){
            if(songList.getSongListName() != null){
                StringBuilder sb = new StringBuilder();
                sb.append("%").append(songList.getSongListName()).append("%");
                songList.setSongListName(sb.toString());
            }
            if(songList.getSongListType() != null && songList.getSongListType().getSongListTypeName() != null){
                StringBuilder sb = new StringBuilder();
                sb.append("%").append(songList.getSongListType().getSongListTypeName()).append("%");
                songList.getSongListType().setSongListTypeName(sb.toString());
            }
            if(songList.getSongListId() != null){
                StringBuilder sb = new StringBuilder();
                sb.append("%").append(songList.getSongListId()).append("%");
                songList.setSongListId(sb.toString());
            }
        }

        PageInfo pageInfo = null;
        if(pageNum > 0 && pageSize >0){
            PageHelper.startPage(pageNum,pageSize);
            if("new".equals(rank)) {
                List<SongList> songListCondition = songListMapper.selectByConditionCreateTime(songList);
                pageInfo = new PageInfo(songListCondition,5);
            }else{
                List<SongList> songListCondition = songListMapper.selectByCondition(songList);
                pageInfo = new PageInfo(songListCondition,5);
            }
        }
        return pageInfo;
    }

    @Override
    public List<SongList> getSongList(SongList songList) {
        List<SongList> list=new ArrayList<SongList>();
        list=songListMapper.selectByCondition(songList);
        return list;
    }

    @Override
    public PageInfo<SongList> findByCondition(String songListName, Integer songListTypeId, Integer songListStatus, int pageNum, int pageSize) {

            SongList songList=new SongList();
            if(songListName !=null && !"".equals(songListName)){
                StringBuilder sb = new StringBuilder();
                sb.append("%").append(songListName).append("%");
                songList.setSongListName(sb.toString());
            }
            if(songListTypeId!=null){
                SongListType songListType=new SongListType();
                songListType.setSongListTypeId(songListTypeId);
                SongListType songListType1=new SongListType();
                songListType1.setSongListType1(songListType);
                songList.setSongListType(songListType1);
            }
            if(songListStatus!=null){
                songList.setSongListStatus(songListStatus);
            }

        PageInfo pageInfo = null;
        if(pageNum > 0 && pageSize >0){
            PageHelper.startPage(pageNum,pageSize);
            List<SongList> list=songListMapper.findByCondition(songList);
            pageInfo = new PageInfo(list,5);
        }
        return pageInfo;
    }

    @Override
    public int updateSongList(Integer songListStatus,String songListId) {
        return songListMapper.updateSongList(songListStatus,songListId);
    }


}
